/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.house.web;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.service.IDataRightService;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IManageAreaService;
import com.ce.pms.house.vo.ManageOrgVO;
import com.ce.pms.house.vo.PrecinctVO;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：管理区ACTION
 * @author 罗军林 创建日期：2008-7-10
 */
public class PrecinctAction extends PmsBaseAction{
	private IManageAreaService manageAreaService;
	private IDataRightService dataRightService;

	/**
	 * 功能描述:初始化管理区增加页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initPrecinctAdd(@Read (key="manageOrgId") Long manageOrgId){
		setManageOrgToRequest(manageOrgId);
		setAttribute("isEdit", false);
	}

	/**
	 * 功能描述:初始管理区修改页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initPrecinctEdit(@Read (key="precinctId") Long precinctId,
								@Read (key="manageOrgId") Long manageOrgId){
		setManageOrgToRequest(manageOrgId);
		PrecinctVO precinctVO=manageAreaService.findPrecinctById(precinctId);
		setAttribute("precinctVO", precinctVO);
		setAttribute("isEdit", true);
	}

	/**
	 * 功能描述:初始化管理区列表页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initPrecinctList(	@Read(key="manageOrgId") Long manageOrgId,
									@Read(key = "ec_crd",defaultValue="15") int pageSize,
									@Read(key = "ec_p",defaultValue="1")	int pageNo)
	{
		Page page=manageAreaService.queryPagePrecinct(manageOrgId, pageNo, pageSize);
		setAttribute("precinctList", page.getResult());
		setAttribute("totalRows", new Integer( (int) page.getTotalCount()));
	}

	/**
	 * 功能描述:增加一个管理区
	 * @param precinctVO
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void addPrecinct(@Read PrecinctVO precinctVO){
		precinctVO.setCreator(this.getUserId());
		manageAreaService.addPrecinct(precinctVO);
	}

	/**
	 * 功能描述:修改一个管理区
	 * @param precinctVO
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void editPrecinct(@Read PrecinctVO precinctVO){
		precinctVO.setCreator(this.getUserId());
		manageAreaService.editPrecinct(precinctVO);
		//数据权限
		dataRightService.saveDataRight(this.getUserId(), this.getUserId(), IDataRightService.DataRightType.PRECINCT, new Long[]{precinctVO.getKeyword()});
	}

	/**
	 * 功能描述:删除一个管理区
	 * @param precinctVO
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void deletePrecinct(@Read (key="precinctId") Long precinctId){
		try{
			manageAreaService.deletePrecinct(precinctId);
		}catch (Exception e) {
			throw new BusinessException("管理区被引用，删除失败",BusinessException.ERROR);
		}
	}

	/**
	 * 功能描述:查询管理区列表
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void listPrecinct(){

	}

	/**
	 * 功能描述:校验管理区名称的唯一性<br>
	 * @param precinctByName
	 * @param oldName<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-15<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkPrecinctName(	@Read (key="precinctCode") String precinctCode,
									@Read (key="oldPrecinctCode") String oldPrecinctCode){
		Long result=manageAreaService.checkPrecinctByCode(precinctCode, oldPrecinctCode);
		if(result<=0){
			renderText("true");
		}else{
			renderText("已经存在");
		}
	}

	/**
	 * 功能描述:校验编号的唯一性<br>
	 * @param precinctByName
	 * @param oldName<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-15<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkprecinctCode(	@Read (key="name") String precinctByName,
									@Read (key="oldName") String oldName){
		Long result=manageAreaService.checkPrecinctByName(precinctByName, oldName);
		if(result<=0){
			renderText("true");
		}else{
			renderText("已经存在");
		}
	}

	/** 功能描述:根据管理区处ID查询管理处并写入request中<br>
	 * @param manageOrgId<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void setManageOrgToRequest(Long manageOrgId) {
		ManageOrgVO manageOrgVO = manageAreaService.findManageOrgById(manageOrgId);
		setAttribute("manageOrgVO", manageOrgVO);
	}

	/******get/set方法**************************************************/

	/**
	 * @return the manageAreaService
	 */
	public IManageAreaService getManageAreaService() {
		return manageAreaService;
	}

	/**
	 * @param manageAreaService the manageAreaService to set
	 */
	public void setManageAreaService(IManageAreaService manageAreaService) {
		this.manageAreaService = manageAreaService;
	}

	public void setDataRightService(IDataRightService dataRightService) {
		this.dataRightService = dataRightService;
	}


}
